// 我的-部门管理-部门成员
const baseUrl = 'https://gsdse.cn/api/KJRecruit';
const app = getApp();
Page({
  data: {
    members: [],
    pages:1,  //显示页数
    pageSize:10,  //一页的数量
    defaultAvatar:"http://frontend.gsdse.cn/gsdkj-recruit/static/img/images/avatar.svg",
    totalMember:0,//成员总数
  },
  editMember(e) {
    wx.showModal({
      title: "提示",
      content: '很抱歉，由于微信官方限制，我们只能在小程序端移出此功能，请到web端使用。不便之处还请谅解。',
      showCancel: false
    })
  },
  deleteMember(e) {
    const that = this
    const index = e.target.dataset.index
    wx.showModal({
      title: '请输入部门认证码',
      editable: true,
      success: res => {
        if (res.confirm) {
          // console.log(res.content);
          let inputpassword = res.content;
          const identification = getApp().data.department.identification
          //验证输入和验证码是否相等
          if(inputpassword === identification){
            //相等，则开始删除
            //获取对应用户信息
            const delMember = that.data.members[index];
            //开删
            wx.request({
              url: getApp().data.domain + '/removeUser',
              method:"get",
              header: {
                "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
              },
              data:{
                userId:delMember.userId,
                departmentId:delMember.departmentId
              },
              success:async function(res){
                console.log(res.data)
                getApp().showSuccess("删除成功！")
                //重新载入页面
                that.setData({
                  pages:1,
                  members:[]
                })
                await that.changePages()
              },
              fail:function(err){
                console.error(err)
                getApp().showError("删除失败！")
              }
            })
          }else{
            //不等，则给提示
            getApp().showError("认证码输入错误！")
          }
        }
      }
    })

  },
  navToaddMember() {
    // wx.redirectTo({
    //   url: '/pages/addMember/addMember',
    // })
    wx.showModal({
      icon: 'none',
      title: '提示',
      content: "由于微信限制，此功能暂不开放，请在web端操作。如给您带来了麻烦实属抱歉。",
      showCancel: false
    })
  },
  //展示不同页数的内容
  async changePages(){
    const that = this
    if(this.data.members.length >= this.data.totalMember){
      getApp().showMessage("已经到底了")
      return
    }
    //显示加载中
    wx.showLoading({
      title: '加载中',
    })
    //获取展示内容
    const page = this.data.pages
    const memberList = await getApp().selectMembers(page, this.data.pageSize, getApp().data.personInfo.departmentId)
    //把展示的内容推入member数组
    let memberData = this.data.members
    memberList.forEach((item,index)=>{
      memberData.push(item)
    })
    this.setData({
      members:memberData,
      pages:page + 1  //页数++
    })
    //加载完毕
    wx.hideLoading()
  },
  async onShow() {
    const that = this
    //获取屏幕大小
    wx.getSystemInfo({  
      success: function(res) {  
        that.setData({
          pageSize:Math.floor(res.windowHeight / 60) 
        })
      }  
    })
    //获取部门人数
    that.setData({
      totalMember:await getApp().selectTotalMember(getApp().data.personInfo.departmentId)
    })
    //加载页面
    this.changePages();
  },
  onReachBottom(){
    this.changePages();
  }
})